package com.shujia.git;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class Demo2Jdbc {
    public static void main(String[] args) throws Exception{

        //1、加载驱动
        Class.forName("com.mysql.jdbc.Driver");

        //2、建立连接
        Connection con = DriverManager.getConnection("jdbc:mysql://master:3306/shujia", "root", "123456");

        //3、执行sql
        PreparedStatement ps = con.prepareStatement("select username,password from user");

        ResultSet resultSet = ps.executeQuery();

        //4、解析返回的结果
        while(resultSet.next()){
            String name = resultSet.getString("username");
            String age = resultSet.getString("password");

            System.out.println(name + "\t" + age);
        }

        //5、关闭连接
        resultSet.close();
        ps.close();
        con.close();
    }
}
